package com.lingyu.jsjx.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingyu.jsjx.domain.*;
import com.lingyu.jsjx.service.*;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/dataAnalysis")
public class DataAnalysisController extends BaseController {
    @Autowired
    private ICourseReformService courseReformService;
    @Autowired
    private ITeachingResearchService teachingResearchService;
    @Autowired
    private IScientificResearchService scientificResearchService;
    @Autowired
    private ISocialServiceService socialServiceService;
    @Autowired
    private IParttimeRecordService parttimeRecordService;
    @Autowired
    private IExpertEmploymentService expertEmploymentService;
    @Autowired
    private IEvaluationService evaluationService;

    /**
     * 运营分析-教学教研
     *
     * @return
     */
    @RequestMapping("/queryTeachingResearch")
    public AjaxResult queryTeachingResearch() {
        JSONObject result = new JSONObject();
        LocalDateTime startYear = LocalDate.of(LocalDate.now().getYear(), 1, 1).atStartOfDay();//今年1月1号
        LocalDate today = LocalDate.now();
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDateTime startOfYesterday = yesterday.atStartOfDay();
        LocalDateTime startOfDay = today.atStartOfDay();
        LocalDateTime endOfDay = today.atTime(LocalTime.MAX);

        // 获取去年的年份
        int lastYear = Year.now().minusYears(1).getValue();
        LocalDateTime startOfLastYear = LocalDateTime.of(lastYear, 1, 1, 0, 0, 0);//去年1月1号
        LocalDateTime endOfLastYear = LocalDateTime.of(lastYear, 12, 31, 23, 59, 59);//去年12月31号
        QueryWrapper<CourseReform> queryWrapper = new QueryWrapper<>();

        //教改项目数
        //今年总数
        queryWrapper.between("create_time", startYear, endOfDay);
        Long courseReformTotal = courseReformService.count(queryWrapper);
        queryWrapper.clear();

        //查询去年总数
        queryWrapper.between("create_time", startOfLastYear, endOfLastYear);
        Long courseReformLastYearTotal = courseReformService.count(queryWrapper);
        queryWrapper.clear();

        //今日总数
        queryWrapper.between("create_time", startOfDay, endOfDay);
        Long courseReformTodayTotal = courseReformService.count(queryWrapper);
        queryWrapper.clear();

        //昨日总数
        queryWrapper.between("create_time", startOfYesterday, startOfDay);
        Long courseReformYesterdayTotal = courseReformService.count(queryWrapper);


        QueryWrapper<TeachingResearch> queryWrapper1 = new QueryWrapper<>();

        //课程创新数
        //今年总数
        queryWrapper1.eq("research_type_id", 83).between("create_time", startYear, endOfDay);
        Long teachingResearchTotal = teachingResearchService.count(queryWrapper1);
        queryWrapper1.clear();

        //查询去年总数
        queryWrapper1.eq("research_type_id", 83).between("create_time", startOfLastYear, endOfLastYear);
        Long teachingResearchLastYearTotal = teachingResearchService.count(queryWrapper1);
        queryWrapper1.clear();

        //今日总数
        queryWrapper1.eq("research_type_id", 83).between("create_time", startOfDay, endOfDay);
        Long teachingResearchTodayTotal = teachingResearchService.count(queryWrapper1);
        queryWrapper1.clear();

        //昨日总数
        queryWrapper1.eq("research_type_id", 83).between("create_time", startOfYesterday, startOfDay);
        Long teachingResearchYesterdayTotal = teachingResearchService.count(queryWrapper1);


        result.put("courseReformTotal", courseReformTotal);
        result.put("courseReformLastYearTotal", courseReformLastYearTotal);
        result.put("courseReformTodayTotal", courseReformTodayTotal);
        result.put("courseReformYesterdayTotal", courseReformYesterdayTotal);
        result.put("teachingResearchTotal", teachingResearchTotal);
        result.put("teachingResearchLastYearTotal", teachingResearchLastYearTotal);
        result.put("teachingResearchTodayTotal", teachingResearchTodayTotal);
        result.put("teachingResearchYesterdayTotal", teachingResearchYesterdayTotal);
        return success(result);
    }

    /**
     * 运营分析-科研管理
     *
     * @return
     */
    @RequestMapping("/queryScientificResearch")
    public AjaxResult queryScientificResearch() {

        LocalDateTime startYear = LocalDate.of(LocalDate.now().getYear(), 1, 1).atStartOfDay();//今年1月1号
        LocalDate today = LocalDate.now();
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDateTime startOfYesterday = yesterday.atStartOfDay();
        LocalDateTime startOfDay = today.atStartOfDay();
        LocalDateTime endOfDay = today.atTime(LocalTime.MAX);

        // 获取去年的年份
        int lastYear = Year.now().minusYears(1).getValue();
        LocalDateTime startOfLastYear = LocalDateTime.of(lastYear, 1, 1, 0, 0, 0);//去年1月1号
        LocalDateTime endOfLastYear = LocalDateTime.of(lastYear, 12, 31, 23, 59, 59);//去年12月31号
        QueryWrapper<ScientificResearch> queryWrapper = new QueryWrapper<>();

        // 今年查询总数
        queryWrapper.between("create_time", startYear, endOfDay);
        Long scientificResearchTotal = scientificResearchService.count(queryWrapper);
        queryWrapper.clear();

        //查询去年总数
        queryWrapper.between("create_time", startOfLastYear, endOfLastYear);
        Long scientificResearchLastYearTotal = scientificResearchService.count(queryWrapper);
        queryWrapper.clear();

        // 今日查询总数
        queryWrapper.between("create_time", startOfDay, endOfDay);
        Long scientificResearchTodayTotal = scientificResearchService.count(queryWrapper);
        queryWrapper.clear();

        // 昨日查询总数
        queryWrapper.between("create_time", startOfYesterday, startOfDay);
        Long scientificResearchYesterdayTotal = scientificResearchService.count(queryWrapper);
        queryWrapper.clear();

        // 获取今年预算
        queryWrapper.between("create_time", startYear, endOfDay);
        BigDecimal budgetTotal = scientificResearchService.list(queryWrapper).stream()
                .filter(Objects::nonNull)  // 过滤掉null值
                .map(ScientificResearch::getBudget)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        queryWrapper.clear();

        // 获取去年预算
        queryWrapper.between("create_time", startOfLastYear, endOfLastYear);
        BigDecimal lastYearBudgetTotal = scientificResearchService.list(queryWrapper).stream()
                .filter(Objects::nonNull)  // 过滤掉null值
                .map(ScientificResearch::getBudget)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        queryWrapper.clear();

        //今日预算
        queryWrapper.between("create_time", startOfDay, endOfDay);
        BigDecimal todayBudgetTotal = scientificResearchService.list(queryWrapper).stream()
                .filter(Objects::nonNull)  // 过滤掉null值
                .map(ScientificResearch::getBudget)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        queryWrapper.clear();

        // 昨日预算
        queryWrapper.between("create_time", startOfYesterday, startOfDay);
        BigDecimal yesterdayBudgetTotal = scientificResearchService.list(queryWrapper).stream()
                .filter(Objects::nonNull)  // 过滤掉null值
                .map(ScientificResearch::getBudget)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        JSONObject result = new JSONObject();
        result.put("scientificResearchTotal", scientificResearchTotal);
        result.put("scientificResearchLastYearTotal", scientificResearchLastYearTotal);
        result.put("scientificResearchTodayTotal", scientificResearchTodayTotal);
        result.put("scientificResearchYesterdayTotal", scientificResearchYesterdayTotal);
        result.put("budgetTotal", budgetTotal);
        result.put("lastYearBudgetTotal", lastYearBudgetTotal);
        result.put("todayBudgetTotal", todayBudgetTotal);
        result.put("yesterdayBudgetTotal", yesterdayBudgetTotal);
        return success(result);
    }

    /**
     * 运营分析-社会服务
     *
     * @return
     */
    @RequestMapping("/querySocialService")
    public AjaxResult querySocialService() {

        JSONObject result = new JSONObject();
        LocalDateTime startYear = LocalDate.of(LocalDate.now().getYear(), 1, 1).atStartOfDay();//今年1月1号
        LocalDate today = LocalDate.now();
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDateTime startOfYesterday = yesterday.atStartOfDay();
        LocalDateTime startOfDay = today.atStartOfDay();
        LocalDateTime endOfDay = today.atTime(LocalTime.MAX);

        // 获取去年的年份
        int lastYear = Year.now().minusYears(1).getValue();
        LocalDateTime startOfLastYear = LocalDateTime.of(lastYear, 1, 1, 0, 0, 0);//去年1月1号
        LocalDateTime endOfLastYear = LocalDateTime.of(lastYear, 12, 31, 23, 59, 59);//去年12月31号
        QueryWrapper<SocialService> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<ParttimeRecord> queryWrapper2 = new QueryWrapper<>();
        QueryWrapper<ExpertEmployment> queryWrapper3 = new QueryWrapper<>();


        // 今年查询总数
        queryWrapper1.between("create_time", startYear, endOfDay);
        queryWrapper2.between("create_time", startYear, endOfDay);
        queryWrapper3.between("create_time", startYear, endOfDay);
        Long socialServiceTotal1 = socialServiceService.count(queryWrapper1);
        Long socialServiceTotal2 = parttimeRecordService.count(queryWrapper2);
        Long socialServiceTotal3 = expertEmploymentService.count(queryWrapper3);
        Long socialServiceTotal = socialServiceTotal1 + socialServiceTotal2 + socialServiceTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();


        //查询去年总数
        queryWrapper1.between("create_time", startOfLastYear, endOfLastYear);
        queryWrapper2.between("create_time", startOfLastYear, endOfLastYear);
        queryWrapper3.between("create_time", startOfLastYear, endOfLastYear);
        Long socialServiceLastYearTotal1 = socialServiceService.count(queryWrapper1);
        Long socialServiceLastYearTotal2 = parttimeRecordService.count(queryWrapper2);
        Long socialServiceLastYearTotal3 = expertEmploymentService.count(queryWrapper3);
        Long socialServiceLastYearTotal = socialServiceLastYearTotal1 + socialServiceLastYearTotal2 + socialServiceLastYearTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();

        // 今日查询总数
        queryWrapper1.between("create_time", startOfDay, endOfDay);
        queryWrapper2.between("create_time", startOfDay, endOfDay);
        queryWrapper3.between("create_time", startOfDay, endOfDay);
        Long socialServiceTodayTotal1 = socialServiceService.count(queryWrapper1);
        Long socialServiceTodayTotal2 = parttimeRecordService.count(queryWrapper2);
        Long socialServiceTodayTotal3 = expertEmploymentService.count(queryWrapper3);
        Long socialServiceTodayTotal = socialServiceTodayTotal1 + socialServiceTodayTotal2 + socialServiceTodayTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();

        // 昨日查询总数
        queryWrapper1.between("create_time", startOfYesterday, startOfDay);
        queryWrapper2.between("create_time", startOfYesterday, startOfDay);
        queryWrapper3.between("create_time", startOfYesterday, startOfDay);
        Long socialServiceYesterdayTotal1 = socialServiceService.count(queryWrapper1);
        Long socialServiceYesterdayTotal2 = parttimeRecordService.count(queryWrapper2);
        Long socialServiceYesterdayTotal3 = expertEmploymentService.count(queryWrapper3);
        Long socialServiceYesterdayTotal = socialServiceYesterdayTotal1 + socialServiceYesterdayTotal2 + socialServiceYesterdayTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();

        //转化率
        queryWrapper1.isNotNull("attachment_file_id").between("create_time", startYear, endOfDay);
        queryWrapper2.isNotNull("achievements").between("create_time", startYear, endOfDay);
        queryWrapper3.isNotNull("guidance_achievements").between("create_time", startYear, endOfDay);
        Long conversionTotal1 = socialServiceService.count(queryWrapper1);
        Long conversionTotal2 = parttimeRecordService.count(queryWrapper2);
        Long conversionTotal3 = expertEmploymentService.count(queryWrapper3);
        Long conversionTotal = conversionTotal1 + conversionTotal2 + conversionTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();

        queryWrapper1.isNotNull("attachment_file_id").between("create_time", startOfLastYear, endOfLastYear);
        queryWrapper2.isNotNull("achievements").between("create_time", startOfLastYear, endOfLastYear);
        queryWrapper3.isNotNull("guidance_achievements").between("create_time", startOfLastYear, endOfLastYear);
        Long conversionLastYearTotal1 = socialServiceService.count(queryWrapper1);
        Long conversionLastYearTotal2 = parttimeRecordService.count(queryWrapper2);
        Long conversionLastYearTotal3 = expertEmploymentService.count(queryWrapper3);
        Long conversionLastYearTotal = conversionLastYearTotal1 + conversionLastYearTotal2 + conversionLastYearTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();

        // 今日查询总数
        queryWrapper1.isNotNull("attachment_file_id").between("create_time", startOfDay, endOfDay);
        queryWrapper2.isNotNull("achievements").between("create_time", startOfDay, endOfDay);
        queryWrapper3.isNotNull("guidance_achievements").between("create_time", startOfDay, endOfDay);
        Long conversionTodayTotal1 = socialServiceService.count(queryWrapper1);
        Long conversionTodayTotal2 = parttimeRecordService.count(queryWrapper2);
        Long conversionTodayTotal3 = expertEmploymentService.count(queryWrapper3);
        Long conversionTodayTotal = socialServiceTodayTotal1 + socialServiceTodayTotal2 + socialServiceTodayTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();

        // 昨日查询总数
        queryWrapper1.isNotNull("attachment_file_id").between("create_time", startOfYesterday, startOfDay);
        queryWrapper2.isNotNull("achievements").between("create_time", startOfYesterday, startOfDay);
        queryWrapper3.isNotNull("guidance_achievements").between("create_time", startOfYesterday, startOfDay);
        Long conversionYesterdayTotal1 = socialServiceService.count(queryWrapper1);
        Long conversionYesterdayTotal2 = parttimeRecordService.count(queryWrapper2);
        Long conversionYesterdayTotal3 = expertEmploymentService.count(queryWrapper3);
        Long conversionYesterdayTotal = conversionYesterdayTotal1 + conversionYesterdayTotal2 + conversionYesterdayTotal3;
        queryWrapper1.clear();
        queryWrapper2.clear();
        queryWrapper3.clear();


        result.put("socialServiceTotal", socialServiceTotal);
        result.put("socialServiceLastYearTotal", socialServiceLastYearTotal);
        result.put("socialServiceTodayTotal", socialServiceTodayTotal);
        result.put("socialServiceYesterdayTotal", socialServiceYesterdayTotal);
        result.put("conversionTotal", conversionTotal);
        result.put("conversionLastYearTotal", conversionLastYearTotal);
        result.put("conversionTodayTotal", conversionTodayTotal);
        result.put("conversionYesterdayTotal", conversionYesterdayTotal);
        return success(result);
    }

    @RequestMapping("/queryMineEvaluation")
    public AjaxResult queryMineEvaluation() {
        JSONObject result = new JSONObject();
        //学生评价
        QueryWrapper<Evaluation> evaluationWrapper = new QueryWrapper<>();
        evaluationWrapper.select("YEAR(create_time) as year",
                        "MONTH(create_time) as month",
                        "COUNT(*) as recordCount",
                        "SUM(total_score) as totalScoreSum")
                .apply("YEAR(create_time) = YEAR(CURDATE())")
                .groupBy("YEAR(create_time)", "MONTH(create_time)")
                .orderByAsc("year", "month");
        if (SecurityUtils.getUserId() != 1L) {
            evaluationWrapper.eq("create_by", SecurityUtils.getUserId());
        }
        List<Map<String, Object>> evaluation = evaluationService.listMaps(evaluationWrapper);


        result.put("evaluation", evaluation);
        return success(result);
    }
}
